/*
* $Id: QBManagerTest.cpp 152 2010-09-23 18:28:55Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "Components/Journal/QueenBeeManager.h"
#include "Sources/Database/QueryResult.h"
#include <cppunit/TestCase.h>
#include<cppunit/extensions/HelperMacros.h>

class CQBManagerTest : public CppUnit::TestCase
{
	CPPUNIT_TEST_SUITE(CQBManagerTest);
	CPPUNIT_TEST(InitTest);
	CPPUNIT_TEST(AddTest);
	CPPUNIT_TEST(ChangeTest);
	CPPUNIT_TEST(DeleteTest);
	CPPUNIT_TEST(ChangeFTest);
	CPPUNIT_TEST(ChangePTest);
	CPPUNIT_TEST_SUITE_END();
private:
	std::vector<CTableItem> GetRow(const std::tstring& strID,
		const std::tstring& strName, const std::tstring& strDescription,
		const std::tstring& strIdFamily, const std::tstring& strIdParent,
		const std::tstring& strDate, const std::tstring& strBreed,
		const std::tstring& strFert, const std::tstring& strDead);
	void Init(CQueenBeeManager* pManager);
public: 
	CQBManagerTest(){};
	void InitTest();
	void AddTest();
	void ChangeTest();
	void DeleteTest();
	void ChangeFTest();
	void ChangePTest();
};

std::vector<CTableItem> CQBManagerTest::GetRow(const std::tstring& strID, const std::tstring& strName, const std::tstring& strDescription,
											   const std::tstring& strIdFamily, const std::tstring& strIdParent, const std::tstring& strDate,
											   const std::tstring& strBreed, const std::tstring& strFert, const std::tstring& strDead){
	std::vector<CTableItem> m_aItems;
	CTableItem item = {_T("id_queen_bee"), strID};
	m_aItems.push_back(item);
	CTableItem item1 = {_T("name"), strName};
	m_aItems.push_back(item1);
	CTableItem item2 = {_T("description"), strDescription};
	m_aItems.push_back(item2);
	CTableItem item3 = {_T("id_family"), strIdFamily};
	m_aItems.push_back(item3);
	CTableItem item4 = {_T("id_parent_queen_bee"), strIdParent};
	m_aItems.push_back(item4);
	CTableItem item5 = {_T("date"), strDate};
	m_aItems.push_back(item5);
	CTableItem item6 = {_T("breed"), strBreed};
	m_aItems.push_back(item6);
	CTableItem item7 = {_T("fert"), strFert};
	m_aItems.push_back(item7);
	CTableItem item8 = {_T("is_dead"), strDead};
	m_aItems.push_back(item8);
	return m_aItems;
}

void CQBManagerTest::Init(CQueenBeeManager* pManager){
	MyTable aTable;
	aTable.push_back(GetRow(_T("1"),_T("1a"),_T("descr"),_T("1"),_T("0"),_T("10.06.2010 01:00:00"),_T(""),_T(""),_T("0")));
	aTable.push_back(GetRow(_T("2"),_T("2a"),_T("descr"),_T("2"),_T("0"),_T("10.06.2010 01:00:00"),_T(""),_T(""),_T("1")));
	aTable.push_back(GetRow(_T("3"),_T("3a"),_T("descr"),_T("2"),_T("2"),_T("10.06.2010 01:00:00"),_T(""),_T(""),_T("0")));
	aTable.push_back(GetRow(_T("4"),_T("4a"),_T("descr"),_T("4"),_T("3"),_T("10.06.2010 01:00:00"),_T(""),_T(""),_T("1")));
	aTable.push_back(GetRow(_T("5"),_T("5a"),_T("descr"),_T("5"),_T("0"),_T("10.06.2010 01:00:00"),_T(""),_T(""),_T("0")));
	aTable.push_back(GetRow(_T("6"),_T("6a"),_T("descr"),_T("4"),_T("4"),_T("10.06.2010 01:00:00"),_T(""),_T(""),_T("0")));
	g_QR.Init();
	g_QR.AddResult(aTable);
	pManager->Init();
}

void CQBManagerTest::InitTest(){
	CQueenBeeManager manager;
	Init(&manager);

	std::map<int, CQueenBeeManager::queen_bee> aQueenBee = manager.GetQueenBees();
	CPPUNIT_ASSERT(aQueenBee.size() == 6);
	CPPUNIT_ASSERT(aQueenBee[1].s_strName == _T("1a"));
	CPPUNIT_ASSERT(aQueenBee[2].s_strName == _T("2a"));
	CPPUNIT_ASSERT(aQueenBee[3].s_strName == _T("3a"));
	CPPUNIT_ASSERT(aQueenBee[4].s_strName == _T("4a"));
	CPPUNIT_ASSERT(aQueenBee[5].s_strName == _T("5a"));
	CPPUNIT_ASSERT(aQueenBee[6].s_strName == _T("6a"));
	aQueenBee = manager.GetFamilyQB(4);
	CPPUNIT_ASSERT(aQueenBee.size() == 2);
	CPPUNIT_ASSERT(aQueenBee[4].s_strName == _T("4a"));
	CPPUNIT_ASSERT(aQueenBee[6].s_strName == _T("6a"));
	aQueenBee = manager.GetChildQB(4);
	CPPUNIT_ASSERT(aQueenBee.size() == 1);
	CPPUNIT_ASSERT(aQueenBee[6].s_strName == _T("6a"));
}

void CQBManagerTest::AddTest(){
	CQueenBeeManager manager;
	Init(&manager);

	MyTable aTable;
	std::vector<CTableItem> aItems;
	CTableItem item = {_T("id_queen_bee"), _T("7")};
	aItems.push_back(item);
	aTable.push_back(aItems);
	g_QR.AddResult(aTable);
	time_t t;
	time(&t);
	manager.AddQueenBee(_T("7a"), _T("descr"), _T(""), t, false, QueenBee::HYBRID, 4);
	std::map<int, CQueenBeeManager::queen_bee> aQueenBee = manager.GetQueenBees();
	CPPUNIT_ASSERT(aQueenBee.size() == 7);
	CPPUNIT_ASSERT(aQueenBee[7].s_strName == _T("7a"));
	aQueenBee = manager.GetFamilyQB(4);
	CPPUNIT_ASSERT(aQueenBee.size() == 3);
	CPPUNIT_ASSERT(aQueenBee[7].s_strName == _T("7a"));

	aTable.clear();
	aItems.clear();
	CTableItem item1 = {_T("id_queen_bee"), _T("8")};
	aItems.push_back(item1);
	aTable.push_back(aItems);
	g_QR.AddResult(aTable);
	manager.AddQueenBee(_T("8a"), _T("descr"), _T(""), t, false, QueenBee::HYBRID, 0);
	aQueenBee = manager.GetQueenBees();
	CPPUNIT_ASSERT(aQueenBee.size() == 8);
	CPPUNIT_ASSERT(aQueenBee[8].s_strName == _T("8a"));
}

void CQBManagerTest::ChangeTest(){
	CQueenBeeManager manager;
	Init(&manager);

	time_t t;
	time(&t);
	manager.ChangeQueenBee(4, _T("4a-new"), _T("descr"), _T(""), t, true, QueenBee::HYBRID);
	std::map<int, CQueenBeeManager::queen_bee> aQueenBee = manager.GetQueenBees();
	CPPUNIT_ASSERT(aQueenBee.size() == 6);
	CQueenBeeManager::queen_bee qb = manager.GetQueenBee(4);
	CPPUNIT_ASSERT(qb.s_strName == _T("4a-new"));
	CPPUNIT_ASSERT(!qb.s_nBreed);
	CPPUNIT_ASSERT(qb.s_bDead);
	CPPUNIT_ASSERT(manager.FindQueenBee(_T("4a-new")) == 4);
}

void CQBManagerTest::DeleteTest(){
	CQueenBeeManager manager;
	Init(&manager);

	manager.DeleteQueenBee(4);
	std::map<int, CQueenBeeManager::queen_bee> aQueenBee = manager.GetQueenBees();
	CPPUNIT_ASSERT(aQueenBee.size() == 5);
	aQueenBee = manager.GetFamilyQB(4);
	CPPUNIT_ASSERT(aQueenBee.size() == 1);
	aQueenBee = manager.GetChildQB(3);
	CPPUNIT_ASSERT(!aQueenBee.size());
	CQueenBeeManager::queen_bee qb = manager.GetQueenBee(6);
	CPPUNIT_ASSERT(!qb.s_nIdParentQueenBee);
}

void CQBManagerTest::ChangeFTest(){
	CQueenBeeManager manager;
	Init(&manager);
	manager.ChangeFamilyQB(2, 1);
	std::map<int, CQueenBeeManager::queen_bee> aQueenBee = manager.GetFamilyQB(1);
	CPPUNIT_ASSERT(aQueenBee.size() == 2);
	CPPUNIT_ASSERT(aQueenBee[2].s_strName == _T("2a"));
}

void CQBManagerTest::ChangePTest(){
	CQueenBeeManager manager;
	Init(&manager);
	manager.ChangeParentQB(4, 2);
	std::map<int, CQueenBeeManager::queen_bee> aQueenBee = manager.GetChildQB(2);
	CPPUNIT_ASSERT(aQueenBee.size() == 2);
	CPPUNIT_ASSERT(aQueenBee[4].s_strName == _T("4a"));
	aQueenBee = manager.GetChildQB(3);
	CPPUNIT_ASSERT(!aQueenBee.size());
}

CPPUNIT_TEST_SUITE_REGISTRATION(CQBManagerTest);